﻿package mortal.game.view.shopMall.view
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import com.gengine.resource.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.mvc.core.*;

    public class GoodsCellRenderer extends GCellRenderer
    {
        private var _shopItem:ShopSellItem;
        private var _yuanjia:GTextFiled;
        private var _xianjia:GTextFiled;
        private var _propName:GTextFiled;
        private var _oldPrice:GTextFiled;
        private var _price:GTextFiled;
        private var _buyBtn:GLoadingButton;
        private var _deleteLine:GBitmap;
        private var _sideIcon:GBitmap;
        private var _listPriceIcon:GBitmap;
        private var _priceIcon:GBitmap;
        private var _hasBuyIcon:GImageBitmap;
        private var _numericStepper:GNumericStepper;
        private var _specialIcon:GImageBitmap;

        public function GoodsCellRenderer()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:GTextFormat = null;
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(5, 4, 240, 105, this, ImagesConst.GoodsBg));
            _loc_1 = GlobalStyle.textFormatlan.right();
            this._oldPrice = UIFactory.gTextField("", 141, 32, 60, 25, this, _loc_1, true);
            this._price = UIFactory.gTextField("", 145, 80, 60, 25, this, _loc_1, true);
            _loc_1 = GlobalStyle.textFormatBai.center();
            this._yuanjia = UIFactory.gTextField(Language.getString(30060), 70, 32, 60, 25, this, _loc_1, true);
            this._listPriceIcon = UIFactory.gBitmap("", 205, 32, this);
            this._deleteLine = UIFactory.gBitmap("", 183, 37, this);
            this._xianjia = UIFactory.gTextField(Language.getString(30061), 85, 55, 60, 25, this, _loc_1, true);
            this._priceIcon = UIFactory.gBitmap("", 205, 55, this);
            _loc_1 = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.CENTER;
            _loc_1.size = 14;
            _loc_1.bold = true;
            this._propName = UIFactory.gTextField("", 64, 6, 120, 25, this, _loc_1, true);
            this._shopItem = UICompomentPool.getUICompoment(ShopSellItem);
            this._shopItem.x = 11;
            this._shopItem.y = 33;
            this._shopItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            this.addChild(this._shopItem);
            this._sideIcon = UIFactory.gBitmap("", 33, 4, this);
            this._buyBtn = UIFactory.gLoadingButton(ResFileConst.ShopMallBuyBtn, 175, 78, 51, 24, this);
            this._buyBtn.configEventListener(MouseEvent.CLICK, this.clickHandler);
            _loc_1.align = AlignMode.RIGHT;
            _loc_1.bold = false;
            this._numericStepper = UIFactory.gNumericStepper(100, 79, 50, 20, this, 99, 1, "NumericStepper", _loc_1);
            this._numericStepper.value = 1;
            LoaderHelp.addResCallBack(ResFileConst.shopMall, this.showSkin);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.ShopLimitItemBuySuccess, this.updateBuyLimit);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateBuyRepet);
            NetDispatcher.removeCmdListener(ServerCommand.SkillAdd, this.updateBuyRepet);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.resetMaxBuyNum);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.resetMaxBuyNum);
            this._oldPrice.dispose(param1);
            this._price.dispose(param1);
            this._shopItem.dispose(param1);
            this._buyBtn.dispose(param1);
            this._propName.dispose(param1);
            this._numericStepper.dispose(param1);
            this._sideIcon.dispose(param1);
            this._listPriceIcon.dispose(param1);
            this._priceIcon.dispose(param1);
            this._deleteLine.dispose(param1);
            this._yuanjia.dispose(param1);
            this._xianjia.dispose(param1);
            this._oldPrice = null;
            this._price = null;
            this._shopItem = null;
            this._buyBtn = null;
            this._propName = null;
            this._numericStepper = null;
            this._sideIcon = null;
            this._listPriceIcon = null;
            this._priceIcon = null;
            this._deleteLine = null;
            this._yuanjia = null;
            this._xianjia = null;
            if (this._hasBuyIcon)
            {
                this._hasBuyIcon.dispose(param1);
                this._hasBuyIcon = null;
            }
            if (this._specialIcon)
            {
                this._specialIcon.dispose(param1);
                this._specialIcon = null;
            }
            super.disposeImpl(param1);
            return;
        }// end function

        public function set isHasItem(param1:Boolean) : void
        {
            if (param1)
            {
                if (this._hasBuyIcon == null)
                {
                    this._hasBuyIcon = UIFactory.gImageBitmap(ImagesConst.ShopMallHasBuyIcon, 12, 40, this);
                }
            }
            else if (this._hasBuyIcon)
            {
                this._hasBuyIcon.dispose();
                this._hasBuyIcon = null;
            }
            return;
        }// end function

        private function showSkin() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._deleteLine.bitmapData = GlobalClass.getBitmapData(ImagesConst.deleteLine);
            return;
        }// end function

        private function resetSpeciclIcon() : void
        {
            var _loc_1:String = null;
            if (ItemsUtil.isRune(this._shopItem.itemData.itemInfo))
            {
                if (RuneCache.calcRunesCombat(this._shopItem.itemData) == 0)
                {
                    _loc_1 = ImagesConst.shopIcon_Random;
                }
                else
                {
                    _loc_1 = ImagesConst.shopIcon_fix;
                }
            }
            if (_loc_1)
            {
                if (this._specialIcon == null)
                {
                    this._specialIcon = UIFactory.gImageBitmap("", 17, 39, this);
                }
                this._specialIcon.imgUrl = _loc_1;
            }
            else if (this._specialIcon)
            {
                this._specialIcon.dispose();
                this._specialIcon = null;
            }
            return;
        }// end function

        private function resetMaxBuyNum(param1:Object = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._shopItem && this._shopItem.shopItemData)
            {
                if (this._shopItem.shopItemData.tShop.unit > 1000)
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getItemCountByItemCode(this._shopItem.shopItemData.tShop.unit, true);
                }
                else
                {
                    _loc_2 = Cache.instance.role.getMoneyByType(this._shopItem.shopItemData.tShop.unit);
                }
                _loc_3 = this._shopItem.shopItemData.prize;
                _loc_4 = _loc_2 / _loc_3;
                this._numericStepper.maximum = _loc_4 > 999 ? (999) : (_loc_4);
            }
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            this._shopItem.shopItemData.num = this._numericStepper.value;
            Dispatcher.dispatchEvent(new DataEvent(EventName.BuyItem, this._shopItem));
            if (GameController.autoGuide.recorder.getCurDoneStep(14) == 3)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:14, stepId:4}));
            }
            return;
        }// end function

        private function setSideIcon() : void
        {
            var _loc_1:String = null;
            if (isDisposed)
            {
                return;
            }
            this._sideIcon.visible = true;
            switch(this._shopItem.shopItemData.tShopSell.label)
            {
                case 1:
                {
                    _loc_1 = ImagesConst.ShopNewItem;
                    break;
                }
                case 2:
                {
                    _loc_1 = ImagesConst.SopHot;
                    break;
                }
                case 3:
                {
                    _loc_1 = ImagesConst.ShopOffer;
                    break;
                }
                default:
                {
                    this._sideIcon.visible = false;
                    return;
                    break;
                }
            }
            this._sideIcon.bitmapData = GlobalClass.getBitmapData(_loc_1);
            return;
        }// end function

        private function setMoneyIcon(param1:int) : void
        {
            var _loc_2:* = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(param1));
            this._priceIcon.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getEPrictUnitImg(param1));
            this._listPriceIcon.bitmapData = _loc_2;
            return;
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            super.selected = param1;
            if (selected)
            {
                EffectManager.glowFilterReg(this, null, 0.7, 5, 0);
            }
            else
            {
                EffectManager.glowFilterUnReg(this);
            }
            return;
        }// end function

        private function updateBuyLimit(param1:Object = null) : void
        {
            var _loc_3:SShopBuyPlayerInfo = null;
            var _loc_2:* = Cache.instance.shop.buyRecords;
            var _loc_4:* = this._shopItem.shopItemData.tShop.shareBuyRecordShop != 0 ? (this._shopItem.shopItemData.tShop.shareBuyRecordShop) : (this._shopItem.shopItemData.tShop.code);
            if (_loc_2[_loc_4])
            {
                _loc_3 = _loc_2[_loc_4][this._shopItem.shopItemData.tShopSell.itemCode] as SShopBuyPlayerInfo;
            }
            this._shopItem.setAmountLabel(Language.getStringByParam(35561, _loc_3 ? (_loc_3.amount) : (0), this._shopItem.shopItemData.tShopSell.maxBuyLimit));
            return;
        }// end function

        private function updateBuyRepet(param1:Object = null) : void
        {
            this.isHasItem = ShopUtil.isHasBuyItem(this._shopItem.itemData);
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            var _loc_3:GTextFormat = null;
            super.data = param1;
            this._shopItem.shopItemData = param1.data as ShopItemData;
            this._shopItem.shopItemData.type = this._shopItem.shopItemData.tShopSell.isBindMoney;
            this._shopItem.amount = 1;
            this.resetSpeciclIcon();
            if (this._shopItem.shopItemData.tShopSell.maxBuyLimit > 0)
            {
                this.updateBuyLimit();
                NetDispatcher.addCmdListener(ServerCommand.ShopLimitItemBuySuccess, this.updateBuyLimit);
            }
            else
            {
                NetDispatcher.removeCmdListener(ServerCommand.ShopLimitItemBuySuccess, this.updateBuyLimit);
            }
            if (this._shopItem.shopItemData.tShopSell.repeatBuy)
            {
                this.isHasItem = false;
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateBuyRepet);
                NetDispatcher.removeCmdListener(ServerCommand.SkillAdd, this.updateBuyRepet);
            }
            else
            {
                this.updateBuyRepet();
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateBuyRepet);
                NetDispatcher.addCmdListener(ServerCommand.SkillAdd, this.updateBuyRepet);
            }
            var _loc_2:* = this._shopItem.shopItemData.tShop;
            LoaderHelp.addResCallBack(ResFileConst.shopMall, this.setSideIcon);
            this.setMoneyIcon(_loc_2.unit);
            this._numericStepper.value = 1;
            if (this._shopItem.shopItemData.tShopSell.price == this._shopItem.shopItemData.tShopSell.offer)
            {
                var _loc_4:Boolean = false;
                this._deleteLine.visible = false;
                var _loc_4:* = _loc_4;
                this._listPriceIcon.visible = _loc_4;
                var _loc_4:* = _loc_4;
                this._yuanjia.visible = _loc_4;
                this._oldPrice.visible = _loc_4;
                this._price.y = 46;
                this._xianjia.y = 46;
                this._priceIcon.y = 46;
            }
            else
            {
                this._oldPrice.text = this._shopItem.shopItemData.tShopSell.price.toString();
                var _loc_4:Boolean = true;
                this._deleteLine.visible = true;
                var _loc_4:* = _loc_4;
                this._listPriceIcon.visible = _loc_4;
                var _loc_4:* = _loc_4;
                this._yuanjia.visible = _loc_4;
                this._oldPrice.visible = _loc_4;
                this._price.y = 55;
                this._xianjia.y = 55;
                this._priceIcon.y = 55;
            }
            if (this._shopItem.shopItemData.tShopSell.activeOffer != 0)
            {
                _loc_3 = GlobalStyle.textFormatItemGreen.right();
                this._price.defaultTextFormat = _loc_3;
                this._xianjia.text = Language.getString(30072);
                this._price.text = this._shopItem.shopItemData.tShopSell.activeOffer.toString();
            }
            else
            {
                _loc_3 = GlobalStyle.textFormatBai.right();
                this._price.defaultTextFormat = _loc_3;
                this._xianjia.text = Language.getString(30061);
                this._price.text = this._shopItem.shopItemData.tShopSell.offer.toString();
            }
            if (this._shopItem.shopItemData.tShop.unit > 1000)
            {
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.resetMaxBuyNum);
                NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.resetMaxBuyNum);
            }
            else
            {
                NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.resetMaxBuyNum);
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.resetMaxBuyNum);
            }
            this._propName.htmlText = ItemsUtil.getItemName(this._shopItem.itemData);
            this.resetMaxBuyNum();
            return;
        }// end function

    }
}
